package com.epoint.mobileoa.bizlogic;


import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;

import com.epoint.mobileoa.R;

/**
 * @author liyc
 * @time 2012-12-27 下午4:09:41
 * @annotation 自定义Tab切换部件Activity 
 */
public class SuperTabView extends SuperView {

	private LinearLayout llTopButton,llContent;
	private LayoutInflater mInflater;
	private boolean[] isInitTask;
	private boolean isShowViewTitleByTabButton;

	@Override
	public void initControls(){
		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		llTopButton = (LinearLayout) findViewById(R.id.llTopButton);
		llContent = (LinearLayout) findViewById(R.id.llContent);
	}

	private void addTab(String text)
	{
		Button btn = new Button(this);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				for(int i=0;i<llTopButton.getChildCount();i++)
				{
					if(llTopButton.getChildAt(i)!=v)
					{
//						llTopButton.getChildAt(i).setBackgroundResource(getTabButtonDrawable()[0]);
//						((Button)llTopButton.getChildAt(i)).setTextColor(getResources().getColor(R.color.white));
//						llContent.getChildAt(i).setVisibility(View.GONE);
						setNotPressed(i);
					}
					else{
						setPressed(i);
					}
				}
			}
		});
		btn.setBackgroundResource(getTabButtonDrawable()[0]);
		btn.setTextColor(getResources().getColor(R.color.white));
		btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
		btn.setText(text);
		btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
		llTopButton.addView(btn);
	}
	
	/**
	 * 根据索引设置要显示的Tab页
	 * @param index
	 */
	public void showTabViewByIndex(int index)
	{
		
		if(llTopButton.getChildCount()<index+1){
			return;
		}
		
		for(int i=0;i<llTopButton.getChildCount();i++)
		{
			if(i!=index)
			{
				setNotPressed(i);
			}
			else{
				setPressed(index);
			}
		}
		
	}

	private void setPressed(int i)
	{
		if(isShowViewTitleByTabButton){
			top_title.setText(((Button)llTopButton.getChildAt(i)).getText());
		}
		llTopButton.getChildAt(i).setBackgroundResource(getTabButtonDrawable()[1]);
		((Button)llTopButton.getChildAt(i)).setTextColor(getResources().getColor(R.color.solid_green));
		llContent.getChildAt(i).setVisibility(View.VISIBLE);
		if(!isInitTask[i]){
			initTask(i);
			isInitTask[i] = true;
		}
	}
	
	private void setNotPressed(int i)
	{
		llTopButton.getChildAt(i).setBackgroundResource(getTabButtonDrawable()[0]);
		((Button)llTopButton.getChildAt(i)).setTextColor(getResources().getColor(R.color.white));
		llContent.getChildAt(i).setVisibility(View.GONE);
	}

	public int initTask(int index)
	{
		return index;
	}

	private void addTabContent(View v)
	{
		llContent.addView(v);
	}

	public void addTabInfo(String[] tabTitles,View[] views)
	{
		for(int i=0;i<tabTitles.length;i++)
		{
			addTabInfo(tabTitles[i], views[i]);
		}
		addTabContentOver();
	}

	private void addTabContentOver()
	{
		isInitTask = new boolean[llContent.getChildCount()];
		for(int i=0;i<llContent.getChildCount();i++)
		{
			if(i!=0)
			{
				llContent.getChildAt(i).setVisibility(View.GONE);
			}
			else{
				setPressed(i);
			}
		}
	}

	private void addTabInfo(String title,View v)
	{
		addTab(title);
		addTabContent(v);
	}

	public View inflate(int resource,ViewGroup root)
	{
		return mInflater.inflate(resource, root);
	}

	@Override
	public void refresh(Object... param) throws Exception {

	}

	@Override
	public void refreshForNewTask(int TaskId, Object... param) {

	}

	@Override
	public int getLayoutInt() {
		return R.layout.supertabview_bottom;
	}

	public int[] getTabButtonDrawable()
	{
		return new int[]{R.drawable.tab_btn_n1,R.drawable.tab_btn_p1};
	}

	/**
	 * 是否显示Tab也标签为标题
	 */
	public boolean isShowViewTitleByTabButton() {
		return isShowViewTitleByTabButton;
	}

	/**
	 * 设置页面标题是否随tab按钮变化
	 * @param isShowViewTitleByTabButton
	 */
	public void setShowViewTitleByTabButton(boolean isShowViewTitleByTabButton) {
		this.isShowViewTitleByTabButton = isShowViewTitleByTabButton;
	}


}
